<#assign security=JspTaglibs["/WEB-INF/security.tld"] />
<!DOCTYPE html>
<html>
<head>
<title>权限管理</title>
<script type="text/javascript">
//新增模块
function popAddOrModify(id) {
    var parentModuleId = "";
    if (id != undefined) {
        parentModuleId = id;
    } else {
        parentModuleId = "ROOT_MODULE"
    }
    $.post("${absoluteContextPath}/system/module/goAdd", {
        'parentModuleId': parentModuleId
    }, function (data) {
        var data = JSON.parse(data);
        if (data.returnCode == 1) {
            var pop_div = $('#myModal');
            pop_div.html(data.content.data);
            pop_div.modal('show');
        } else {
            $.showMessage({parentId: "showMessage", message: data.returnMsg, type: "error"});
        }
    });
}

//编辑模块
function popEditOrModify(id) {
    var data = {};
    var url = '${absoluteContextPath}/system/module/goEdit';
    if (id != undefined) {
        data.id = id;
    }
    $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                var pop_div = $('#myModal');
                pop_div.html(data.content.data);
                pop_div.modal('show');
            } else {
                $.showMessage({parentId: "showMessage", message: "数据加载异常！", type: "success"});
            }
        }
    });
}

//删除模块
function deleteModule(id, moduleId) {
    $.confirm({
        confirm: function () {
            $("#confirmModal").modal("hide");
            $.post("${absoluteContextPath}/system/module/delete", {
                'id': id,
                'moduleId': moduleId
            }, function (data) {
                data = JSON.parse(data);
                if (data.returnCode == 1) {
                    $.showMessage({parentId: "showMessage", message: "模块数据删除成功！", type: "success"});
                    refreshModuleTable();
                } else {
                    $.showMessage({parentId: "showMessage", message: "请先删除子模块，再删除该菜单！", type: "error"});
                }
            });
        }
    });
}

//刷新模块
function refreshModuleTable() {
    $.post("${absoluteContextPath}/system/module/ajaxList", {
    }, function (data) {
        var data = JSON.parse(data);
        if (data.returnCode == 1) {
            $("#tableContent").html(data.content.data);
        } else {
            $.showMessage({parentId: "showMessage", message: "表格数据加载异常！", type: "success"});
        }
    });
}

//刷新权限列表
function refreshAuth() {
    var selectModuleId = $("#selectModuleId").val();
    $.post("${absoluteContextPath}/system/authData/ajaxListByModuleId", {
        'moduleId': selectModuleId
    }, function (data) {
        var data = JSON.parse(data);
        if (data.returnCode == 1) {
            $("#tableContent2").html(data.content.data);
        } else {
            $.showMessage({parentId: "showMessage", message: "表格数据加载异常！", type: "success"});
        }
    });
}

//新增权限
function addModuleAuth() {
    var selectModuleId = $("#selectModuleId").val();
    if (selectModuleId != "") {
        $.post("${absoluteContextPath}/system/authData/goAdd", {
            'moduleId': selectModuleId
        }, function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                var pop_div = $('#myModal');
                pop_div.html(data.content.data);
                pop_div.modal('show');
            } else {
                $.showMessage({parentId: "showMessage2", message: "数据加载异常！", type: "success"});
            }
        });
    } else {
        $.showMessage({parentId: "showMessage2", message: "请先选择模块，才能进行新增操作！", type: "error"});
    }
}

//编辑权限
function modifyModuleAuth(id) {
    var selectModuleId = $("#selectModuleId").val();
    if (selectModuleId != "") {
        $.post("${absoluteContextPath}/system/authData/goEdit", {
            'id': id
        }, function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                var pop_div = $('#myModal');
                pop_div.html(data.content.data);
                pop_div.modal('show');
            } else {
                $.showMessage({parentId: "showMessage2", message: "数据加载异常！", type: "success"});
            }
        });
    } else {
        $.showMessage({parentId: "showMessage2", message: "请先选择模块，才能进行新增操作！", type: "error"});
    }
}

//删除权限
function deleteModuleAuth() {
    var moduleId = $("#selectModuleId").val();
    var i = 0;
    $("input[name=ids]").each(function () {
        if ($(this).prop("checked")) {
            i++;
        }
    });
    if (i == 0) {
        $.showMessage({parentId: "showMessage2", message: "请选择您要删除的记录！", type: "success"});
        return false;
    } else {
        $.confirm({
            confirm: function () {
                var values = "";
                $("#authTable input[name=ids]").each(function () {
                    if ($(this).prop("checked")) {
                        values += $(this).val() + ",";
                    }
                });
                $("#confirmModal").modal("hide");
                $.post("${absoluteContextPath}/system/authData/delete", {
                    'ids': values,
                    'moduleId': moduleId
                }, function (data) {
                    data = JSON.parse(data);
                    if (data.returnCode == 1) {
                        $.showMessage({parentId: "showMessage2", message: "数据删除成功！", type: "success"});
                        refreshAuth();
                    } else {
                        $.showMessage({parentId: "showMessage2", message: "数据删除异常！", type: "error"});
                    }
                });
            }
        });
    }
}

//分配资源
function popAssign(authId) {
    var data = {};
    var url = '${absoluteContextPath}/system/authData';
    $("#selectAuthId").val(authId);
    data.authId = authId;
    data.moduleId = $("#selectModuleId").val();
    data.pageSize = 6;
    url = url + "/goAssign";
    $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                var pop_div = $('#myModal');
                pop_div.html(data.content.data);
                pop_div.modal('show');
            } else {
                $.showMessage({parentId: "showMessage", message: "数据加载异常！", type: "success"});
            }
        }
    });
}

//新增或编辑资源
function popAddOrModifyResource(id) {
    var data = {};
    var url = '${absoluteContextPath}/system/resource';
    if (id != undefined) {
        data.id = id;
        url = url + "/goEdit";
    } else {
        url = url + "/goAdd";
    }

    data.moduleId = $("#selectModuleId").val();
    data.authId = $("#search_assign_resource_table input[name=authId]").val();

    $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                var pop_div = $('#myModal2');
                pop_div.html(data.content.data);
                pop_div.modal('show');
            } else {
                $.showMessage({parentId: "errorMessage", message: "数据加载异常！", type: "success"});
            }
        }
    });
}

//删除资源
function deleteResource() {
    var i = 0;
    $("input[name=resourceIds]").each(function () {
        if ($(this).prop("checked")) {
            i++;
        }
    });
    if (i == 0) {
        $.showMessage({parentId: "showMessage", message: "请选择您要删除的记录！", type: "success"});
        return false;
    } else {
        $.confirm({
            confirm: function () {
                var values = "";
                $("input[name=resourceIds]").each(function () {
                    if ($(this).prop("checked")) {
                        values += $(this).val() + ",";
                    }
                });
                $("#confirmModal").modal("hide");
                $.post("${absoluteContextPath}/system/resource/delete", {
                    'ids': values
                }, function (data) {
                    data = JSON.parse(data);
                    if (data.returnCode == 1) {
                        refreshAssignResourceTable();
                        $.showMessage({parentId: "errorMessage", message: "数据删除成功！", type: "success"});
                    } else {
                        $.showMessage({parentId: "errorMessage", message: "数据删除异常！", type: "error"});
                    }
                });
            }
        });
    }
}

//刷新分配资源列表
function refreshAssignResourceTable(currentPage) {
    if (currentPage) {
        $("input[name=currentPage]").val(currentPage);
    }
    $("#search_assign_resource_table").ajaxSubmit({
        url: '${absoluteContextPath}/system/authData/ajaxAssignResourceList',
        beforeSend: showLoading,
        success: function (data) {
            var data = JSON.parse(data);
            if (data.returnCode == 1) {
                $("#assignResourceTableContent").html(data.content.data);
            } else {
                $.showMessage({parentId: "showMessage", message: "表格数据加载异常！", type: "success"});
            }
        }
    });
}

</script>
</head>
<body>

<div class="breadcrumbs">
    <ol class="breadcrumb">
        <li><a href="${absoluteContextPath}/home"><i class="fa fa-home"></i>首页</a></li>
        <li><a href="#">用户管理</a></li>
        <li class="active">模块权限管理</li>
    </ol>
</div>


<div class="main" id="main">
    <!-- row -->
    <div class="row">
        <div class="col-md-4">
            <section class="tile">
                <div class="tile-body nopadding">
                    <div id="showMessage"><#include "../../message.ftl" /></div>
                    <div class="table-responsive" id="tableContent">
                    <#include "table.ftl"/>
                    </div>
                </div>
            </section>
        </div>
        <div class="col-md-8">
            <section class="tile">
                <div class="tile-header">
                    <h1>
                    <@security.authorize ifAnyGranted="ROLE_AUTH_A">
                        <button type="button" class="btn btn-green" onclick="addModuleAuth();"><i
                                class="fa fa-plus"></i><span class="margin-left-5">新增</span></button>
                    </@security.authorize>
                    <@security.authorize ifAnyGranted="ROLE_AUTH_D">
                        <button type="button" class="btn btn-cyan" onclick="deleteModuleAuth();"><i
                                class="fa fa-trash-o"></i><span class="margin-left-5">删除</span></button>
                    </@security.authorize>
                    </h1>
                </div>
                <div class="tile-body nopadding">
                    <div id="showMessage2"><#include "../../message.ftl" /></div>
                    <div class="table-responsive" id="tableContent2">
                    <#include "moduleAuthTable.ftl"/>
                    </div>
                </div>
            </section>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    </div>
    <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="cardCode04"
         aria-hidden="true">
    </div>
</div>

<div style="display: none">
    <input id="roleId" type="hidden" value="${roleId!''}"/>
</div>
</body>
</html>
